﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using MaxLeaf.DB;
using System.Data;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
 

namespace MaxLeaf.DAL.Web2SCP
{
   

    public class goods_mainDAL : BaseDAL
    {
        public goods_mainDAL()
            : base()
        {
            strSCPTableName = "goods_main";
            strWebTableName = "goods_main";

            strSCPKeyFieldName = "goods_id";
            strWebKeyFieldName = "goods_id";

            strFieldNames = "goods_name,cat_top_id,cat_id,brand_id,spec,sku_num,market_price,goods_desc,goods_details,goods_summary,click_count,inventory_status,delivery_cycle,main_image_id,unit_group_id,sale_type,give_integral,sale_area,goods_type,is_recommend,is_on_sale,is_delete,sort_order,update_time,create_time,delete_time,market_price_desc,saving_index,min_buynum";
 
        }


        public override int Update(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestSCPCon, string strKeyValue)
        {
            int intDestKeyValue = base.Update(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestSCPCon, strKeyValue);

            KeyValuePair<string, IDataParameter[]> kvp = dbDestSCPCon.CreateCondParam(new Dictionary<string, object>() { { strSCPKeyFieldName, intDestKeyValue } });
            DataRow drData = dbDestSCPCon.GetDataFirstRow(string.Format("select * from {1} where {0}", kvp.Key, strSCPTableName), kvp.Value);
            new ImInvMasterDAL().RefreshData(dbDestSCPCon, CvtFunc.ToInt(drData["goods_id"]));
            

            return intDestKeyValue;
        }

        protected override void DAL_GetFieldData(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestSCPCon, string strSCPFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strSCPFieldName.Equals("unit_group_id", StringComparison.InvariantCultureIgnoreCase))
            {
                base.DAL_GetFieldData(dbOrgWebReadCon,   dbOrgWebWriteCon,   dbDestSCPCon,   strSCPFieldName,   drOrgData,   drDestData);

                new goods_unit_groupDAL().Update(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestSCPCon, CvtFunc.ToString(drDestData[strSCPFieldName]));
            }
            else if (strSCPFieldName.Equals("is_delete", StringComparison.InvariantCultureIgnoreCase))
            {
                if (CvtFunc.ToBoolean(drDestData[strSCPFieldName]) != CvtFunc.ToBoolean(drOrgData[strSCPFieldName]))
                {
                    new ImInvMasterDAL().UpdateDelete(dbDestSCPCon, CvtFunc.ToInt(drDestData["goods_id"]), CvtFunc.ToBoolean(drOrgData[strSCPFieldName]));
                }
                base.DAL_GetFieldData(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestSCPCon, strSCPFieldName, drOrgData, drDestData);

                
            }
            else
            {
                base.DAL_GetFieldData(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestSCPCon, strSCPFieldName, drOrgData, drDestData);
            }
        }
    }
}